import {login }from "@/api/users.js"
import ElementUI from 'element-ui';
import {setToken} from "@/utils/auth.js"
const state = {
    token:setToken(),
}
const mutations={
    SET_TOKEN:(state,token)=>{
        state.token=token
    },
}
const actions={
    getLogin({commit},userInfo){
        console.log(commit);
        let {name,password}=userInfo
        return new Promise(resolve => {
            login({ name: name.trim(), password: password }).then(res=>{
                let result=res.data.filter((item)=>{ return item.name==name&&item.password==password})
                if(result.length){
                    localStorage.setItem("token",result[0].token)
                    localStorage.setItem("user",result[0].name)
                    commit("SET_TOKEN",result[0].token)
                    setToken(result[0].token)
                    resolve()
                }else{
                    ElementUI.Message.error("账号或密码错误");
                }
                
            })
    
        })
      
    },
}
export default {
        namespaced: true,
        state,
        mutations,
        actions
}
